<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>供应商公告消息日志主表 管理</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<form id="splBullMsgLM_form">
    <div class="fieldset1 tabel_col tabel_col_1 clearFix">
        
<div class="fieldBox col_100">
	<div class="label">消息ID</div>
	<div class="field">
		<input name="msgId" id="msgId" type="text" class="ui-textbox" value="${splBullMsgLMVO.msgId}" validate="{required:true,number:true}">
		<span class="requiredSymbol">*</span>
	</div>
</div>
<div class="fieldBox col_100">
	<div class="label">群发部门</div>
	<div class="field">
		<c:forEach items="${orgList}" var="item">
			<input type="checkbox"   class="ui-checkbox bu-org" 
			<c:forEach items="${orgCds}" var="it">
				<c:if test="${it==item.key}">
	   				checked
				</c:if>
			 </c:forEach>
			orgNm="${item.value }" value="${item.key}"/>${item.value }
		</c:forEach>
	</div>
</div>
<div class="fieldBox col_100">
	<div class="label">群发地区</div>
	<div class="field">
		<input name="rgnCyCd" id="rgnCyCd" type="text" value="" placeholder="请选择" ltype="combobox">
	</div>
</div>
<div class="fieldBox col_50">
	<div class="label">群发对象</div>
	<div class="field">
		<input type="text" id="splObj" name="splObj" ltype="combobox" placeholder="请选择"/>
	</div>
</div>
<div class="fieldBox col_100">
	<div class="label">提醒方式</div>
	<div class="field">
		<input type="checkbox" id="isTips" name ="isTips" 	
		<c:if test="${splBullMsgLMVO.isTips!=0}">
	   		checked
		</c:if>
		class="ui-checkbox" />APP提示
	</div>
</div>
    </div>
    <div class="btnsBox">
        <span id="saveSplBullMsgLM_Btn" class="sq-button sq-button-round l-dialog-btn-ok">保存</span>
        
	   		<span id="push_bull_Btn" class="sq-button sq-button-round l-dialog-btn-ok"
	   		<c:if test="${splBullMsgLMVO.pubStsCd!=1}">
	   		style=display:none
	   		</c:if>
	   		>发布</span>
		
        <input type="reset" id="resetBtn" class="l-button l-button-reset Hide">
        <input type="hidden" id="rgnList" name="rgnList" class="">
        <input type="hidden" id="splList" name="splList" class="">
        <input type="hidden" id="splIds" name="splIds" class="">
        <input type="hidden" id="buOrgList" name="buOrgList" class="">
        <input type="hidden" id="buOrgCds" name="buOrgCds" class="">
        <input type="hidden" id="rgnPrCds" name="rgnPrCds" class="">
        <input type="hidden" id="rgnCyCds" name="rgnCyCds" class="">
        <input type="hidden" id="splIdsSum" name="splIdsSum" class="">  
    </div>
</form>
<script>
var rgnList = [];
var splList =[];
var buOrgList =[];
var treeselect;
var buOrgCds =[];
var rgnPrCds=[];
var rgnCyCds =[];
var splIds =[];
var splBullMsgLMId = "${splBullMsgLMId}";//主键ID
var dataSc ="";
//返回数据
var splIds = "${splIds}";
var rgnCds = "${rgnCds}";
    $(function() {
        //群发对象
    	var _splObjJSON = null;
    	$("#splObj").ligerComboBox({ 
    		isShowCheckBox: true, isMultiSelect: true,
    		initIsTriggerEvent: false,
            data: _splObjJSON,
            valueField: 'splId',
            textField:'splNm',
            onChangeValue : function (value)  {
            	var vl = splManager.getValue().split(";");
            	var tl = splManager.getText().split(";");
            	splList=[];
            	splIds=[];
            	for(var k=0;k<vl.length;k++){
            		for(var j=0;j<tl.length;j++){
            			if(j==k){
            				var spl={};
            				spl.splId=vl[k];
            				spl.splNm=tl[j];
            				splIds.push(vl[k]);
            				splList.push(spl);
            			}
            		}
            	}
            	/* alert(JSON.stringify(splList)); */
            	$('#splList').val(JSON.stringify(splList));
            	$('#splIds').val(JSON.stringify(splIds));
             }
        });
    	var splManager = $("#splObj").ligerGetComboBoxManager();
    	//获取部门数据
    	 $(".bu-org").change(function (event) {
    		 buOrgCds=[];
    		 buOrgList=[];
    		 getSelectOrg();
    	        //$('#buOrgCds').val(JSON.stringify(buOrgCds));
    	       // $('#buOrgList').val(JSON.stringify(buOrgList));
    	        searchSplList();
    	      //alert(JSON.stringify(buOrgCds));
    	      //alert(JSON.stringify(buOrgList));
    	 });
    	
    	 $("#push_bull_Btn").click(function (event) {
    		 var form = liger.get("splBullMsgLM_form");
             if (!form.valid()) {
                 $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                 return false; //验证不通过，不执行以下代码
             }
            var data = form.getData();
            data.buOrgCds = $('#buOrgCds').val();
            data.rgnPrCds = $('#rgnPrCds').val();
            data.rgnCyCds = $('#rgnCyCds').val();
            console.log($('#splIdsSum').val());
            if($('#splIds').val()=="" || $('#splIds').val()== null){
            	var splIdsSum =[];
            	for(var o in dataSc){  
            		splIdsSum.push(dataSc[o].splId);	
                }
            	$('#splIdsSum').val("");
            	$('#splIdsSum').val(JSON.stringify(splIdsSum));
            	data.splIds = $('#splIdsSum').val();
            }else{
            	data.splIds = $('#splIds').val();
            }
            /* alert(data.splIds) */
			/* alert(data.rgnCyCds) */
            data.logId = splBullMsgLMId;
     		//alert(JSON.stringify(data));return;
     		var wait_dialog;
     		var _url = '<%=path%>/splmsg/splBullMsgLM/pushMessage.do';
             $.ajax({
                 type: 'post',
                 dataType: 'json',
                 data: data,
                 url: _url,
                 beforeSend: function () {
                     wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                 },
                 success: function (msg) {
                	 wait_dialog.close();
                     if (msg.success == 1) {
                    	 //发布成功 
                         $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                         document.getElementById("push_bull_Btn").style.display="none";
                         setInterval('Remove_navTab();callback();',1000);
                     } else {
                         $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                     }
                 }
             });
    	 });
    	
    	/*保存*/
        $("#saveSplBullMsgLM_Btn").click(function (event) {
            var form = liger.get("splBullMsgLM_form");
            if (!form.valid()) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                return false; //验证不通过，不执行以下代码
            }
            var data = form.getData();
            if($('#splIds').val()=="" || $('#splIds').val()== null){
            	var splIdsSum =[];
            	for(var o in dataSc){  
            		splIdsSum.push(dataSc[o].splId);	
                }
            	$('#splIdsSum').val("");
            	$('#splIdsSum').val(JSON.stringify(splIdsSum));
            	data.splList = $('#splIdsSum').val();
            	console.log(data.splList)
            }else{
            	data.splList = $('#splList').val();
            }
            data.buOrgList = $("#buOrgList").val();
            data.rgnList = $("#rgnList").val();
            data.isTips = $('#isTips').is(':checked')?1:0;//1是 0否
            data.pubFlag = 0;//1是 0否 --决定有无发布时间
            //alert(JSON.stringify(data));return;
            $.ligerDialog.hide();
            var _url;
            if (splBullMsgLMId == '') {//新增
                _url = '<%=path%>/splmsg/splBullMsgLM/add.do';
            } else {//修改
                _url = '<%=path%>/splmsg/splBullMsgLM/modify.do?id=' + splBullMsgLMId;
            }
            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: data,
                url: _url,
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                        //回调刷新列表
                        setInterval('Remove_navTab();callback();',1000);
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        });
        $("#rgnCyCd").ligerComboBox({
            // width: 178,
            selectBoxWidth: 200,
            selectBoxHeight: 150,
            treeLeafOnly: true,  //限制只能选叶节点，父节点不能点击
            textField:"rgnNm",
            valueField:"rgnCd",
            tree: {
                url: '<%=path%>/project/prjQuatPrpsAttr/getRegionTreeData.do',
                ajaxType: 'get',
                checkbox: true,
                isExpand: false,
                parentIcon: "none",
                childIcon: "none",
                textFieldName:"rgnNm",
                idFieldName:"rgnCd",
                onSuccess: function () {
                    treeselect=liger.get("rgnCyCd");
                }
            },
            onSelected : function(value,text){
            	getSelectRgn();//将所选区域放入指定input
            	searchSplList();
            }
        });
        
        /* $('#isTips').change(function(){
        	if($(this).checked){
        		$(this).val('1');//是
        	}else{
        		$(this).val('0');//否
        	}
        }); */
        
        
        initData();
        
        $("#splBullMsgLM_form").ligerForm({
            validate: true
        });
    });
  //*****script*********************************************************************************************
  //获取所选群发部门
  function getSelectOrg(){
	  obj = $('.bu-org');
      for(k in obj){
          if(obj[k].checked){
          	var org={};
          	org.buOrgNm = obj[k].getAttribute('orgNm');
          	org.buOrgCd = obj[k].value;
          	buOrgList.push(org);
          	buOrgCds.push(obj[k].value);
          }
      }
      $('#buOrgCds').val(JSON.stringify(buOrgCds));
      $('#buOrgList').val(JSON.stringify(buOrgList));
  }
  //获取群发地区方法
    function getSelectRgn(){
    	rgnList =[];
    	rgnPrCds=[];
    	rgnCyCds =[];
    	var manager = liger.get("rgnCyCd").treeManager;
    	var notes =  manager.getChecked();
    	for(var i = 0;i<notes.length;i++){
    		var rgn ={};
    		var p = manager.getParent(notes[i]);
    		if(p!=null){
    			rgn.rgnPrCd = p.rgnCd;
    			rgn.rgnPrNm = p.rgnNm;
    			rgn.rgnCyCd = notes[i].data.rgnCd;
    			rgn.rgnCyNm = notes[i].data.rgnNm;
    			rgnCyCds.push(notes[i].data.rgnCd);
    			rgnPrCds.push(p.rgnCd);
    			rgnList.push(rgn);
             }
    	}
    	$('#rgnList').val(JSON.stringify(rgnList));
    	$('#rgnPrCds').val(JSON.stringify(rgnPrCds));
    	$('#rgnCyCds').val(JSON.stringify(rgnCyCds));
    	//alert(rgnPrCds+"||"+rgnCyCds);
    	
    }
    function searchSplList(){
		var paramData = {};
		paramData.buOrgCds = $('#buOrgCds').val();
		paramData.rgnPrCds = $('#rgnPrCds').val();
		paramData.rgnCyCds = $('#rgnCyCds').val();
		//alert(JSON.stringify(paramData));
		var _url = '<%=path%>/splmsg/splBullMsgLM/getSplList.do';
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: paramData,
            url: _url,
            success: function (msg) {
            	var datas = JSON.stringify(msg)
            	dataSc = ""	
            	dataSc = eval('(' + datas + ')');
                $("#splObj").ligerGetComboBoxManager().setData(msg);
                //$("#splObj").ligerGetComboBoxManager().updateStyle();
            }
        });
	}
    
    function initData(){
		if(!splBullMsgLMId){
			//alert('主键id为空');
			return;
		}
    	var _url = '<%=path%>/splmsg/splBullMsgLM/getById.do';
    	$.ajax({
    		type: 'get',
    		dataType:'json',
    		data:{id:splBullMsgLMId},
    		url: _url,
    		success:function(msg){
    			 if(msg.success == 1){
    				 setData(msg.data);//设置数据
    				 //console.log(msg.data);
    			 }else{
    				 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
    			 }
    		}
    	});
   	}
		
	function setData(data){
		//初始化
		/*  var orgCds = data.orgCds;
		 $(".bu-org").each(function(){
			    for(i in orgCds){
	        		if($(this).val() ==orgCds[i]){
	        			$(this).checked = true;
	        			//alert(orgCds[i]);
	        			alert($(this).val());
	        		}
	        	}
		}); 
		$('#msgId').val(data.splBullMsgLMVO.msgId);
		if(data.splBullMsgLMVO.isTips==1){
			$('#isTips').attr("checked","");
		}
		*/
		$("#rgnCyCd").ligerGetComboBoxManager().selectValue(rgnCds);
		$("#rgnCyCd").ligerGetComboBoxManager().updateStyle();
		$('#rgnCyCds').val(rgnCds);
		
		$("#splObj").ligerGetComboBoxManager().selectValue(splIds);
		$("#splObj").ligerGetComboBoxManager().updateStyle();
		$('#buOrgCds').val('${orgCds}');
		$('#splIds').val('${splIds}');
		getSelectOrg();
		getSelectRgn();
		searchSplList();
	}
</script>
</body>
</html>
